﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using CuteSprite.Eme.Client;
using CuteSprite.Eme.Data;
using CuteSprite.Eme.Data.BsdUserCenter;
using CuteSprite.Eme.Data.CRM;
using CuteSprite.Eme.Data.Testing;
using CuteSprite.Eme.MIS.Client;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.Components.Controls;
using HmFramework.Components.Editors.DateTimeAdv;
using HmFramework.Data;
using HmFramework.Data.Configuration;
using HmFramework.Log;
using HmFramework.Text;
using HmFramework.UI.Utils;
using HmFramework.UI.Utils.Controls;
using HmFramework.Reflection;
using HmFramework.Threading;


namespace CuteSprite.Eme.UI.Test
{
	public partial class FrmVote : EmeEntityForm
	{

		#region -- Fields --
		private Test005 _CurrentExpressEntity;


		private string _strSelect = "";


		private string _friendName = "";
		#endregion

		#region -- 构造 --

		#region - 新增 -

		/// <summary>默认构造函数</summary>
		public FrmVote()
			: base()
		{
			InitializeComponent();
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="templateFileName">模板文件</param>
		public FrmVote(String templateFileName)
		{
			SetupFromXml(templateFileName);
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="resourceName">模板资源文件</param>
		/// <param name="asm">模板资源文件所在程序集</param>
		public FrmVote(String resourceName, Assembly asm)
		{
			SetupFromXmlResource(resourceName, asm);
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="stream">模板数据流</param>
		public FrmVote(Stream stream)
		{
			SetupFromXmlStream(stream);
			InitForm();
		}

		#endregion

		#region - 编辑或新增 -

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		public FrmVote(IEntity entity)
			: this(entity, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmVote(IEntity entity, Boolean readOnly)
			: base(entity, readOnly)
		{
			InitializeComponent();
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="templateFileName">模板文件</param>
		public FrmVote(IEntity entity, String templateFileName)
			: this(entity, templateFileName, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="templateFileName">模板文件</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmVote(IEntity entity, String templateFileName, Boolean readOnly)
			: base(entity, readOnly)
		{
			SetupFromXml(templateFileName);

			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="resourceName">模板资源文件</param>
		/// <param name="asm">模板资源文件所在程序集</param>
		public FrmVote(IEntity entity, String resourceName, Assembly asm)
			: this(entity, resourceName, asm, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="resourceName">模板资源文件</param>
		/// <param name="asm">模板资源文件所在程序集</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmVote(IEntity entity, String resourceName, Assembly asm, Boolean readOnly)
			: base(entity, readOnly)
		{
			SetupFromXmlResource(resourceName, asm);

			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="stream">模板数据流</param>
		public FrmVote(IEntity entity, Stream stream)
			: this(entity, stream, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="stream">模板数据流</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmVote(IEntity entity, Stream stream, Boolean readOnly)
			: base(entity, readOnly)
		{
			SetupFromXmlStream(stream);

			InitForm();
		}

		#endregion

		#endregion


		#region -- 属性 --

		private EmeBsdMisManagement _BsdMisManagement = null;

		/// <summary>BsdMIS客户端管理</summary>
		protected EmeBsdMisManagement BsdMisManagement
		{
			get
			{
				if (_BsdMisManagement == null) { _BsdMisManagement = ServerManagement as EmeBsdMisManagement; }
				return _BsdMisManagement;
			}
		}

		private int MaxMultiSelectNumber { get; set; }

		private bool Ifvalid { get; set; }


		private Dictionary<string, int> _voteListDictionary = new Dictionary<string, int>();


		internal Dictionary<string, int> VoteListDictionary
		{
			get { return _voteListDictionary; }
			set
			{
				_voteListDictionary = value;
		
			}
		}


		private DataTable _VoteTable = null;

		private DataTable VoteTable
		{
			get
			{
				if (_VoteTable == null)
				{
					EmeEntityList list = null;
					try
					{
						lock (ServerManagement.LockSynchronizer)
						{
							// 只获取显示字段和值字段
							//list = ServerManagement.Client.FindEntityListWithClause(EntityHelper.ModelTest, EntityHelper.UserCenterTableStaff, null, null, "ID,FullName", 0, 0);
							list = ServerManagement.Client.FindEntityListWithClause("EmeTesting", "VoteOption", null, null, "ID,Describe", 0, 0);
						}
					}
					catch (Exception ex) { DialogHelper.ShowWarning(null, ex.Message); }
					if (list != null && list.Count > 0)
					{
						_VoteTable = list.Entities.Tables[0];
					}
				}
				return _VoteTable;
			}
			//set { _StaffTable = value; }
		}

	
		#endregion

		#region -- 窗体事件 --

		#region - method InitComponent -

		/// <summary>控件初始化</summary>
		protected override void InitComponent()
		{
			base.InitComponent();

			//if (frmSelectID == null)
			//{
			//	frmSelectID = FindControl("frmSelectID") as EntitySelector;
			//}

			//MaxMultiSelectNumber = 1;
			//MessageBox.Show("这个"+"InitComponent()");

			this.OnValid += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmVote_OnValid);
			this.OnSaving += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmVote_OnSaving);
			//this.OnSaveSuccess += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmExpress_OnSaveSuccess);
		}

		#endregion

		#region - method FormOnLoad -


		public override void FormOnLoad()
		{
			
			base.FormOnLoad();
			//AcceptButton 
			this.AcceptButton = FindControl("btnSave") as IButtonControl;
			MaxMultiSelectNumber = 1;

			_friendName = BsdMisManagement.CurrentUser.FriendName;

			foreach (DataRow VARIABLE in (VoteTable.Rows))
			{
				VoteListDictionary.Add(VARIABLE[1].ToString(),Convert.ToInt32(VARIABLE[0]));
			}

			if (MaxMultiSelectNumber > 1)
			{
				foreach (var i in VoteListDictionary)
				{
					CheckBox cbx = new CheckBox();


					cbx.Text = i.Key;
					cbx.Tag = i.Value;

					flpSelect.Controls.Add(cbx);
				}
			}
			else
			{
				foreach (var i in VoteListDictionary)
				{
					RadioButton rbButton = new RadioButton {Text = i.Key, Tag = i.Value};


					flpSelect.Controls.Add(rbButton);
				}
			}
			//VoteListDictionary
			//var tbOption = EmeRemotingHelper.MisServerManagement.OptionInfo.GetTable("VoteOption").Copy();
			
			//
		}

		#endregion

		#region - method FormOnPreLoad -

		protected override void FormOnPreLoad()
		{
			base.FormOnPreLoad();

			//MessageBox.Show("这个" + "FormOnPreLoad()");

			//MyListBoxItems mylistitems = new MyListBoxItems("这是一个测试项，值为100", 100);
			//CheckBoxX cbx = new CheckBoxX();
			//cbx.Text = "这是一个测试项，值为100";
			//cbx.Tag = 100;
			//flpSelect.Controls.Add(cbx);

			//CheckBox cb = new CheckBox();
			//cb.Text = "这是一个测试项2，值为200";
			//cb.Tag = 200;
			//flpSelect.Controls.Add(cb);
		}
		#endregion

		#region - method InitCurrentEntityWhenNewAdd -

		protected override void InitCurrentEntityWhenNewAdd()
		{
			base.InitCurrentEntityWhenNewAdd();
			//MessageBox.Show("这个" + "InitCurrentEntityWhenNewAdd()");
			_CurrentExpressEntity = CurrentEntity as Test005;

			//_CurrentExpressEntity.Name = string.Format("哈哈，我是" + "默认的");
		}
		#endregion
		

		#region - method FormOnShown -

		public override void FormOnShown()
		{
			base.FormOnShown();
			//MessageBox.Show("这个" + " FormOnShown()");
		
		
		}

		#endregion

		#region - 刷新窗体 -

		public override void ResetForm()
		{
		
		}

		private void RefreshFieldControls()
		{
			
		}

		#endregion

		#region - 实体保存 -

		private void FrmVote_OnSaving(object sender, EntityFormEventArgs e)
		{
			//MessageBox.Show(frmSelectID_.SelectedIDs);
			//if (!Ifvalid)
			//{
			//	e.Cancel = true;
			//	return	;
			//}
		

			var UserName = BsdMisManagement.CurrentUser.Name;

			
			//_CurrentExpressEntity.Name = "傻傻不懂爱";
			_CurrentExpressEntity.Name = _friendName;

			_CurrentExpressEntity.SelectID = _strSelect;

			//ReloadEntities();
			//var Name=


		}

		#endregion

		#region - 实体验证 -

		private void FrmVote_OnValid(object sender, EntityFormEventArgs e)
		{
			Ifvalid = false;

			#region 验证用户选项数量

			var i = 0;
			_strSelect = "";
			if (MaxMultiSelectNumber > 1)
			{

				foreach (CheckBox VARIABLE in flpSelect.Controls)
				{
					if (VARIABLE.Checked)
					{
						if (_strSelect == "")
						{
							_strSelect += Convert.ToInt32(VARIABLE.Tag);
						}
						else
						{
							_strSelect += "," + Convert.ToInt32(VARIABLE.Tag);
						}
						i++;
					}
				}
			}
			else
			{

				foreach (RadioButton VARIABLE in flpSelect.Controls)
				{
					if (VARIABLE.Checked)
					{
						_strSelect += Convert.ToInt32(VARIABLE.Tag);
						i++;
					}
				}
			}

			if (i > MaxMultiSelectNumber)
			{
				MessageBox.Show(string.Format("最多选择{0}项", MaxMultiSelectNumber));
				e.Cancel = true;
				return;

			}

			#endregion



			#region 验证用户是否已经投票

			EmeEntityList list = null;
			try
			{
				lock (ServerManagement.LockSynchronizer)
				{
					// 只获取显示字段和值字段
					//list = ServerManagement.Client.FindEntityListWithClause(EntityHelper.ModelTest, EntityHelper.UserCenterTableStaff, null, null, "ID,FullName", 0, 0);
					list = ServerManagement.Client.FindEntityListWithClause("EmeTesting", "Test005", "Name='" + _friendName+"'", null, "Name", 0, 0);
				}
			}
			catch (Exception ex)
			{
				DialogHelper.ShowWarning(null, ex.Message);
			}

			if (list != null && list.Count > 0)
			{
				
				MessageBoxEx.Show("你已经投过票了！");
				e.Cancel = true;
				return;
			}

			#endregion

			

		}

		#endregion

		#region - 实体保存成功 -

		private String m_recStaffValue;


		private void FrmExpress_OnSaveSuccess(object sender, EntityFormEventArgs e)
		{
			
		}

		#endregion

		#endregion

		#region -- 控件事件 --
		 //private override void oncl
		private void CheckBox_CheckChange(object sender, EventArgs e)
		{
			string selectid = "";
			//check
			//foreach (CheckBox mylist in flpSelect.Controls)
			//{
			//	CheckBoxX cbx = mylist as CheckBoxX;
			//	if(selectid=="")
			//	{
			//		selectid += mylist.Tag.ToString();
			//	}else
			//	{
			//		selectid += "," + mylist.Tag.ToString();
			//	}
			//}
			//SelectedIDs=selectid;

			//MessageBox.Show(SelectedIDs.ToString());
		}

		#endregion

		private void button1_Click(object sender, EventArgs e)
		{
			//var tbOption = EmeRemotingHelper.MisServerManagement.LoadEntityByKey(CurrentEntityOperate, "EmeTesting", "VoteOption", 1);

			//MessageBox.Show(len tbOption );
			MessageBox.Show(VoteTable.Rows[0][0].ToString());
		}

		#region -- 辅助 --

	
		#endregion
	}

}
